package com.bocai.yuweiwangluo.activity;


import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.PopBean;
import com.bocai.yuweiwangluo.presenter.PublishTalkPresenter;
import com.bocai.yuweiwangluo.utils.MyConst;
import com.bocai.yuweiwangluo.utils.PopUtil;
import com.bocai.yuweiwangluo.viewModel.IPublishTalkView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

/**
 * 发表话题界面
 */
public class PublishTalkActivity extends BaseActivity implements View.OnClickListener,IPublishTalkView
{
    @ViewInject(R.id.ll_publishtalk_choseball)
    LinearLayout llChoseBall;
    @ViewInject(R.id.tv_publishtalk_choseball)
    TextView tvChoseBall;
    @ViewInject(R.id.et_publishtalk_title)
    EditText etTitle;
    @ViewInject(R.id.et_publishtalk_content)
    EditText etContent;
    @ViewInject(R.id.btn_publishtalk)
    Button btnPublishtalk;
    private int width;
    private PopUtil pu;
    private ArrayList<PopBean> list;
    private String type_id="";
    private PublishTalkPresenter publishTalkPresenter;
    public static final int REQUST_OK=-1;
    @Override
    protected int contentView()
    {
        return R.layout.activity_publish_talk;
    }

    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        getPublishTalkPresenter();
        initToolbar("发表话题");
        WindowManager windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        width=windowManager.getDefaultDisplay().getWidth() ;
        initEvent();
    }

    @Override
    protected void initEvent()
    {
        super.initEvent();
        llChoseBall.setOnClickListener(this);
        btnPublishtalk.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_publishtalk_choseball:
                showPopwindow(v,tvChoseBall);
                break;
            case R.id.btn_publishtalk:
                //发表话题
                publishTalkPresenter.publishTalk();
                break;
        }
    }
    private void showPopwindow(View v,final TextView tv)
    {
        pu= PopUtil.getInstance(this, v, width, 310,setTypeList());
        pu.setListener(
                new PopUtil.OnDatePopClickListener()
                {
                    @Override
                    public void onClick(PopUtil pu, PopBean data)
                    {
                        tv.setText(data.getName());
                        type_id=data.getId();
                        pu.dismiss();
                    }
                });
        pu.show();
    }
    private ArrayList<PopBean> setTypeList() {
        list=new ArrayList<>();
        for (int i=0;i<MyConst.bbstype.size();i++)
        {
            PopBean popBean=new PopBean();
            popBean.setId(MyConst.bbstype.get(i).getId());
            popBean.setName(MyConst.bbstype.get(i).getTitle());
            list.add(i,popBean);
        }
        return list;
    }

    @Override
    public String getContent() {
        return etContent.getText().toString();
    }
    @Override
    public String getTitles() {
        return etTitle.getText().toString();
    }
    @Override
    public String getType_id() {
        return type_id;
    }

    @Override
    public void callback() {
        setResult(REQUST_OK);
    }

    @Override
    public void finishActivity() {
        this.finish();
    }

    @Override
    public PublishTalkPresenter getPublishTalkPresenter() {
        publishTalkPresenter=new PublishTalkPresenter(this,this,this);
        return publishTalkPresenter;
    }
}
